package com.ldj.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.NewsInfo;
import com.ldj.hibernate.NewsInfoDAO;
import com.ldj.hibernate.PersionFile;
import com.ldj.hibernate.PersionFileDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.NewsInfoForm;

public class NewsInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			NewsInfoForm nForm = (NewsInfoForm)form;
			NewsInfoDAO dao = new NewsInfoDAO(request, response);
			String startDate = request.getParameter("newsInfoStartDate");
			String endDate = request.getParameter("newsInfoEndDate");
			List list = dao.findAllPage(startDate,endDate,nForm);
			request.setAttribute("intPage", String.valueOf(dao.intPage));
			request.setAttribute("list", list);
			return mapping.findForward("list");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			return mapping.findForward("toAdd");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsInfoForm nForm = (NewsInfoForm)form;
		NewsInfo info = new NewsInfo();
		info.setNewsInfoTitle(nForm.getNewsInfoTitle());
		info.setNewsInfoType(nForm.getNewsInfoType());
		info.setNewsInfoSourcr(nForm.getNewsInfoSourcr());
		info.setNewsInfoKeyword(nForm.getNewsInfoKeyword());
		info.setNewsInfoContent(nForm.getNewsInfoContent());
		info.setNewsInfoPoint(0L);	
		info.setNewsInfoDelOper("0");	
		info.setNewsInfoDate(new Date());	
		NewsInfoDAO newsInfoDao = new NewsInfoDAO(request, response);
		PersionFileDAO persionFiledao = new PersionFileDAO(request, response);
		PersionFile persionInfo = persionFiledao.findById(SystemSession.getSupUserId(request));
		info.setPersionFile(persionInfo);
		newsInfoDao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "NewsInfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getNewsInfoTitle()));
		logDao.save(s);
		return new ActionForward("/NewsInfo.do?method=list", true);
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsInfoForm nForm = (NewsInfoForm)form;
		NewsInfoDAO newsInfoDao = new NewsInfoDAO(request, response);
		NewsInfo ninfo = newsInfoDao.findById(nForm.getNewsInfoId());
		request.setAttribute("ninfo", ninfo);
		return mapping.findForward("detail");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		NewsInfoForm nForm = (NewsInfoForm)form;
		NewsInfoDAO dao = new NewsInfoDAO(request, response);
		NewsInfo info = dao.findById(nForm.getNewsInfoId());
		request.setAttribute("nInfo", info);	
		request.setAttribute("nowPage",request.getParameter("page"));
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsInfoForm nForm = (NewsInfoForm)form;
		NewsInfoDAO dao = new NewsInfoDAO(request, response);
		NewsInfo info = dao.findById(nForm.getNewsInfoId());
		info.setNewsInfoContent(nForm.getNewsInfoContent());
		info.setNewsInfoKeyword(nForm.getNewsInfoKeyword());
		info.setNewsInfoType(nForm.getNewsInfoType());
		info.setNewsInfoSourcr(nForm.getNewsInfoSourcr());
		info.setNewsInfoTitle(nForm.getNewsInfoTitle());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "NewsInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getNewsInfoTitle()));
		logDao.save(s);
		return new ActionForward("/NewsInfo.do?method=list&pages="+request.getParameter("pages"), true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsInfoForm nForm = (NewsInfoForm)form;
		NewsInfoDAO newsInfoDao = new NewsInfoDAO(request, response);
		NewsInfo ninfo = newsInfoDao.findById(nForm.getNewsInfoId());
		ninfo.setNewsInfoDelOper("1");
		newsInfoDao.merge(ninfo);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "NewsInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", ninfo.getNewsInfoTitle()));
		logDao.save(s);
		return new ActionForward("/NewsInfo.do?method=list&pages="+request.getParameter("page"), true);
	}

}